package com.lanren.familybookapp.ui.home.presenter;

import com.lanren.common.base.impl.BasePresenter;
import com.lanren.common.utils.StringUtils;
import com.lanren.familybookapp.ui.home.bean.BookClassifyBean;
import com.lanren.familybookapp.ui.home.bean.BookPayTypeBean;
import com.lanren.familybookapp.ui.home.interfaces.IBookkeepingAddModel;
import com.lanren.familybookapp.ui.home.model.BookkeepingAddModel;
import com.lanren.familybookapp.ui.me.bean.BookInfoBean;

import java.util.List;

/**
 * @ClassName BookkeepingAddPresenter
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2020/2/14 13:45
 * @Version 1.0
 */
public class BookkeepingAddPresenter extends BasePresenter<IBookkeepingAddModel.IBookkeepingEditView> {
    private BookkeepingAddModel bookkeepingAddModel;

    public BookkeepingAddPresenter(BookkeepingAddModel bookkeepingAddModel) {
        this.bookkeepingAddModel = bookkeepingAddModel;
    }

    public void getBookInfo(int bookId) {
        bookkeepingAddModel.getBookInfo(bookId, new IBookkeepingAddModel.BookInfoModel() {
            @Override
            public void onError(int code, String msg) {
                mRootView.showError(code, msg);
            }

            @Override
            public void onSuccess(List<BookInfoBean> bookInfoBeans) {
                mRootView.onSuccessBookInfo(bookInfoBeans);
            }
        });
    }

    public void addBookkeeping(int bookId, String bookClassify, String bookMoney, String bookInOut, String bookGoods,
                               String bookTime, String bookMember, String bookPaytype, String bookPs) {
        if (bookClassify.equals("无")) {
            mRootView.showMessage("分类不能为无");
            return;
        }
        if (StringUtils.isBlank(bookGoods)) {
            mRootView.showMessage("消费物品不能为空");
            return;
        }
        if (StringUtils.isBlank(bookMoney)) {
            mRootView.showMessage("金额不能为空");
            return;
        }
        if (StringUtils.isBlank(bookMember)) {
            mRootView.showMessage("记账成员不能为空");
            return;
        }
        if (StringUtils.isBlank(bookPaytype)) {
            mRootView.showMessage("支付方式不能为空");
            return;
        }

        bookkeepingAddModel.addBookkeeping(bookId, bookClassify, bookMoney, bookInOut, bookGoods, bookTime,
                bookMember, bookPaytype, bookPs, new IBookkeepingAddModel.BookkeepingEditModel() {
                    @Override
                    public void onError(int code, String msg) {
                        mRootView.showError(code, msg);
                    }

                    @Override
                    public void onSuccess(String msg) {
                        mRootView.onSuccessBook(200, msg, -1);
                    }
                });
    }

    public void getBookClassify(String bookCreateUser) {
        if (StringUtils.isBlank(bookCreateUser)) {
            mRootView.showMessage("创建人不能为空");
            return;
        }
        bookkeepingAddModel.getBookClassify(bookCreateUser, new IBookkeepingAddModel.BookClassifyModel() {
            @Override
            public void onError(int code, String msg) {
                mRootView.showError(code, msg);
            }

            @Override
            public void onSuccess(List<BookClassifyBean> bookClassifyBeans) {
                mRootView.onSuccessClassify(bookClassifyBeans);
            }
        });
    }

    public void getBookPaytypeList(String bookCreateUser) {
        if (StringUtils.isBlank(bookCreateUser)) {
            mRootView.showMessage("创建人不能为空");
            return;
        }
        bookkeepingAddModel.getBookPaytypeList(bookCreateUser, new IBookkeepingAddModel.BookPaytypeModel() {
            @Override
            public void onError(int code, String msg) {
                if (code != 10009) {
                    mRootView.showError(code, msg);
                }
            }

            @Override
            public void onSuccess(List<BookPayTypeBean> bookPayTypeBeans) {
                mRootView.onSuccessPayType(bookPayTypeBeans);
            }
        });
    }

    public void addBookPaytype(String bookCreateUser, String payType) {
        if (StringUtils.isBlank(payType)) {
            mRootView.showMessage("支付方式不能为空");
            return;
        }
        bookkeepingAddModel.addBookPaytype(bookCreateUser, payType, new IBookkeepingAddModel.BookkeepingEditModel() {
            @Override
            public void onError(int code, String msg) {
                mRootView.showError(code, msg);
            }

            @Override
            public void onSuccess(String msg) {
                mRootView.onSuccessBook(100, msg, -1);
            }
        });
    }

    public void delBookPaytype(int payTypeId, final int bookPayTypeNum) {
        if (StringUtils.intIsNull(payTypeId)) {
            mRootView.showMessage("支付方式Id不能为空");
            return;
        }
        bookkeepingAddModel.delBookPaytype(payTypeId, new IBookkeepingAddModel.BookkeepingEditModel() {
            @Override
            public void onError(int code, String msg) {
                mRootView.showError(code, msg);
            }

            @Override
            public void onSuccess(String msg) {
                mRootView.onSuccessBook(101, msg, bookPayTypeNum);
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (bookkeepingAddModel != null) {
            bookkeepingAddModel.clearDisposable();
            bookkeepingAddModel = null;
        }
    }
}
